package net.hn.hnms.sys.common.core.constant;

import cn.hutool.core.util.StrUtil;
import org.springframework.beans.BeanUtils;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * Author: PanSX
 * Date: 2023/4/4 9:00
 */
public class MineBaseConstants {
    public static Map<String, Mine> mineMap = new HashMap() {{
        put("620824009734", new Mine("砚北煤矿", "华亭公司", "620824009734", "916200002258722645", "井工", "一级"));
        put("150621011038", new Mine("高头窑煤矿", "北能公司", "150621011038", "91150100767874571R", "井工", "一级"));
        put("150781013671", new Mine("灵露煤矿", "扎煤公司", "150781013671", "91150781720169744B", "井工", "一级"));
        put("621025009717", new Mine("核桃峪煤矿", "庆阳公司", "621025009717", "91621000566400003E", "井工", "一级"));
        put("150622004330", new Mine("魏家峁煤矿", "北能公司", "150622004330", "91150100767874571R", "露天", "一级"));
        put("150622004317", new Mine("铧尖煤矿", "北能公司", "150622004317", "91150100767874571R", "露天", "一级"));
        put("150781013670", new Mine("灵东煤矿", "扎煤公司", "150781013670", "91150781720169744B", "井工", "一级"));
        put("150781013603", new Mine("灵泉煤矿", "扎煤公司", "150781013603", "91150781720169744B", "井工", "一级"));
        put("150781013194", new Mine("铁北煤矿", "扎煤公司", "150781013194", "91150781720169744B", "井工", "一级"));
    }};
    public static Mine group=new Mine("华能集团", "华能集团", "91110000592360113C", "91110000592360113C", "", "");
    private static Map<String, Mine> codeAndNameOfMap = new HashMap<>();



    public static class Mine {
        private String mineName;
        private String companyName;
        private String mineCode;

        private String companyCode;
        private String type;//露天或井工

        private String standardLevel;

        private List<Mine> list=new ArrayList<>();

        public String getMineName() {
            return mineName;
        }

        public void setMineName(String mineName) {
            this.mineName = mineName;
        }

        public String getCompanyName() {
            return companyName;
        }

        public void setCompanyName(String companyName) {
            this.companyName = companyName;
        }

        public String getMineCode() {
            return mineCode;
        }

        public void setMineCode(String mineCode) {
            this.mineCode = mineCode;
        }

        public String getCompanyCode() {
            return companyCode;
        }

        public void setCompanyCode(String companyCode) {
            this.companyCode = companyCode;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getStandardLevel() {
            return standardLevel;
        }

        public List<Mine> getList() {
            return list;
        }

        public void setList(List<Mine> list) {
            this.list = list;
        }

        public void setStandardLevel(String standardLevel) {
            this.standardLevel = standardLevel;
        }

        public Mine(String mineName, String companyName, String mineCode, String companyCode, String type, String standardLevel) {
            this.mineName = mineName;
            this.companyName = companyName;
            this.mineCode = mineCode;
            this.companyCode = companyCode;
            this.type = type;
            this.standardLevel = standardLevel;
        }

        public Mine() {

        }

        public static Map<String, Mine> getCodeAndNameOfMap() {
            return codeAndNameOfMap;
        }

    }

    public static Map<String, Mine> getNameByCompanyCodeOfMap(String companyCode, String companyName) {
        for (String key : mineMap.keySet()) {
            if (companyCode != null && !companyCode.equals("")) {
                if (codeAndNameOfMap.size() == 0) {
                    if (StrUtil.equalsIgnoreCase(mineMap.get(key).companyCode, companyCode)) {
                        codeAndNameOfMap.put(key, mineMap.get(key));
                    }
                }

            } else if (companyName != null && !companyName.equals("")) {
                if (codeAndNameOfMap.size() == 0) {
                    if (StrUtil.equalsIgnoreCase(mineMap.get(key).companyName, companyName)) {
                        codeAndNameOfMap.put(key, mineMap.get(key));
                    }
                }
            } else if (companyName != null && companyCode != null && !companyName.equals("") && !companyCode.equals("")) {
                if (codeAndNameOfMap.size() == 0) {
                    if (StrUtil.equalsIgnoreCase(mineMap.get(key).companyName, companyName) && StrUtil.equalsIgnoreCase(mineMap.get(key).companyCode, companyCode)) {
                        codeAndNameOfMap.put(key, mineMap.get(key));
                    }
                }
            } else {
                codeAndNameOfMap.put(key, mineMap.get(key));


            }
        }
        Set<String> idNumbers = new HashSet<>();
        Map<String, Mine> uniqueMineMap = new HashMap<>();
        for (Map.Entry<String, Mine> entry : codeAndNameOfMap.entrySet()) {
            String idNumber = entry.getValue().getCompanyCode();
            if (!idNumbers.contains(idNumber)) {
                uniqueMineMap.put(entry.getKey(), entry.getValue());
                idNumbers.add(idNumber);
            }
        }


        return codeAndNameOfMap = uniqueMineMap;
    }

    public static List<Mine> getMineMapByCompanyCode(String companyCode) {
        return mineMap.values().stream().filter(s -> companyCode.equals(s.getMineCode())).collect(Collectors.toList());
    }
    public static List<Mine> getCompanyCode() {
        Map<String, Mine> map=new HashMap<>();
        for (String key : mineMap.keySet()) {
            map.put(mineMap.get(key).getCompanyCode(),new Mine(mineMap.get(key).getMineName(), mineMap.get(key).getCompanyName(), mineMap.get(key).getCompanyCode(), mineMap.get(key).getCompanyCode(), "", ""));
        }
        return new  ArrayList<Mine>(map.values());
    }

    public static Mine getCascadeMine(){
        Mine groups = new Mine();
        BeanUtils.copyProperties(group,groups);
        groups.setList(getCompanyCode());
        for (Mine company:groups.getList()){
            company.setList(getMineMapByCompanyCode(company.getCompanyCode()));
        }
        return groups;
    }
}
